﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Big2.Big2WcfService;
using System.Windows.Threading;
using Big2.Big2Game;

namespace Big2.Big2WfpGui
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : UserControl
    {
        public delegate void ReadyToLoginEventHandler(Connect c, Rules r);
        public event ReadyToLoginEventHandler ReadyToLogin;
        string myIP = "33";

        public Login()
        {
            InitializeComponent();
        }

        private void btConnect_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(tbName.Text))
            {
                MessageBox.Show("Please enter a name");
                return;
            }

            if ((bool)rbClient.IsChecked)  //client
            {
                //validate
                IPAddress ipAddress;
                if (IPAddress.TryParse(tbServer.Text,out ipAddress) == false)
                {
                    MessageBox.Show("Please enter a valid server ip address.");
                    return;
                }

            }
            else  //server
            {
                //validate
            }

            if (ReadyToLogin != null)
            {
                Connect c = new Connect(tbName.Text, tbServer.Text, Constants.ServerPort, rbServer.IsChecked.Value);
                if (rbServer.IsChecked.Value)
                    ReadyToLogin(c, GetRules());
                else
                    ReadyToLogin(c, null);
            }
            
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //string ip = Dns.GetHostByName(Dns.GetHostName().ToString()).AddressList[0].Address.ToString();
            //Dispatcher.Invoke(DispatcherPriority.DataBind, (Action)delegate { InitServerIPTextBox(); });
            InitServerIPTextBox();
        }

        public void InitServerIPTextBox()
        {
            //try
            //{
            //    System.Net.WebClient myWebClient = new System.Net.WebClient();
            //    System.IO.Stream myStream = myWebClient.OpenRead("http://www.geekpedia.com/ip.php");
            //    System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myStream);
            //    myIP = myStreamReader.ReadToEnd();
            //    tbServer.Text = myIP;                
            //}
            //catch
            //{
            //    tbServer.Text = "Unabled to find server ip.  Try http://www.geekpedia.com/ip.php";               
            //}
            tbServer.IsReadOnly = true;
        }

        private void rbServer_Checked(object sender, RoutedEventArgs e)
        {
            if (tbServer != null)
            {
                //tbServer.IsReadOnly = true; ;
                tbServer.Text = myIP;
            }
            if(controlOptions!=null)
                controlOptions.Visibility = Visibility.Visible;
        }

        private void rbClient_Checked(object sender, RoutedEventArgs e)
        {
            if (tbServer != null)
            {
                tbServer.IsReadOnly = false ;
                tbServer.Text = "";
            }
            if (controlOptions != null)
                controlOptions.Visibility = Visibility.Hidden;
            
        }

        public Rules GetRules()
        {
            return controlOptions.getRules();
        }

    }
}
